/* Class PathQueue
 * 
 * 
 */
package disasterGameProj;

public class PathQueue
{
	public PathNode[] pQueue = null;
	
	
	PathQueue()
	{
		pQueue = new PathNode[400];
	}
	
	// returns the last index value 
	public int getLastIndex()
	{
		int counter = 0;
		while( pQueue[counter] != null)
		{
			counter++;
		}
		
		return counter;
		
	}// end method getLastIndex 
	
	public boolean pathNodeExists( Tile tile )
	{
		boolean returnVal = false;
		for( int i = 0; i < getLastIndex(); i++ )
		{
			if( pQueue[i].myTile == tile )
				returnVal = true;
		}
		return returnVal;
	}
	
	public boolean isEmpty()
	{
		if( pQueue[0] == null )
			return true;
		else
			return false;
	}
	
	public void addPathNode( PathNode newPathNode )
	{	
		int index = getLastIndex();
		
		if(index > 0)
		{
			int parentIndex = index;
			if(parentIndex % 2 == 0)
				parentIndex -= 2;
			else
				parentIndex -= 1;
			parentIndex /= 2;
			
			if(pQueue[parentIndex].f_Score > newPathNode.f_Score)		//if need swap
			{
				pQueue[index] = pQueue[parentIndex];
				pQueue[parentIndex] = null;
				shiftNodeUp( newPathNode, parentIndex );
			}
			else
			{
				pQueue[index] = newPathNode;
			}
		}
		else
		{
			pQueue[index] = newPathNode;
		}
	}
	
	public void shiftNodeUp( PathNode newPathNode, int toIndx )
	{	
		int index = toIndx;
		
		if(index > 0)
		{
			int parentIndex = index;
			if(parentIndex % 2 == 0)
				parentIndex -= 2;
			else
				parentIndex -= 1;
			parentIndex /= 2;
			
			if(pQueue[parentIndex].f_Score > newPathNode.f_Score)		//if need swap
			{
				pQueue[index] = pQueue[parentIndex];
				pQueue[parentIndex] = null;
				shiftNodeUp( newPathNode, parentIndex );
			}
			else
			{
				pQueue[index] = newPathNode;
			}
		}
		else
		{
			pQueue[index] = newPathNode;
		}
	}
	
	// moves the last queue item to the first element, and moves it back down to where it really belongs, also acts as a mini-heap sort
	public void replacePathNode( PathNode newPathNode, int toIndx)		//place and reorder PathNodes according to heaps
	{
		int highestPriority = 0;		//1 = left child, 2 = right	child
		
		int leftChildPriority = -1;
		int rightChildPriority = -1;
		
		if(pQueue[toIndx*2 + 1] != null)
		{
			leftChildPriority = pQueue[toIndx*2 + 1].f_Score;
		}
		if(pQueue[toIndx*2 + 2] != null)
		{
			rightChildPriority = pQueue[toIndx*2 + 2].f_Score;
		}
		
		if(leftChildPriority != -1)
		{
			highestPriority = 1;
			
			if(rightChildPriority != -1)
			{
				if(leftChildPriority > rightChildPriority)
					highestPriority = 2;
			}
		}
		
		if(highestPriority == 0)
		{
			pQueue[toIndx] = newPathNode;
		}
		else if(pQueue[toIndx*2 + highestPriority].f_Score < newPathNode.f_Score)
		{
			pQueue[toIndx] = pQueue[toIndx*2 + highestPriority];
			pQueue[toIndx*2 + highestPriority] = null;
			replacePathNode(newPathNode, (toIndx*2 + highestPriority) );
		}
		else
			pQueue[toIndx] = newPathNode;
	
	}// end method replacePathNode
	
	// pops the top priority path off of the queue, and then calls replaceHeapEvent,
	// and then calls the probability logic, and also the asks the user if they wish to 
	// add an path to the queue, unless the stop button is pressed, then it only pops the 
	// paths in order
	public  PathNode pullFromHeap( )
	{
		if( pQueue[0] != null)
		{	
			int lastIndx = getLastIndex() - 1;
			
			PathNode pulledPathNode = pQueue[0];
			pQueue[0] = null;
			
			if( pQueue[lastIndx] != null)
			{
				PathNode temp = pQueue[lastIndx];
				pQueue[lastIndx] = null;
				replacePathNode(temp, 0);
			}
			
			return pulledPathNode;
		}// end if
		else
			return null;
			
	}// end method pulledFromHeap
}